﻿function RituCode() {
    var MIN_LAT = 0;
    var MAX_LAT = 90;
    var MIN_LNG = 72;
    var MAX_LNG = 171;
    var METERS_PER_DEGREE = 109801.71;
    var originLng = 72;
    var originLat = 0;
    var grid10w = 72;
    var grid10h = 72;
    var grid9w = 8;
    var grid9h = 8;
    var PRECISION = 1000000;
    var grid9Lng = parseInt(grid9w * PRECISION / METERS_PER_DEGREE) / PRECISION;
    var grid9Lat = parseInt(grid9h * PRECISION / METERS_PER_DEGREE) / PRECISION;

    this.isValidLatitude = function(lat) {
        if (lat >= MIN_LAT && lat <= MAX_LAT) {
            return true;
        }

        return false;
    }

    this.isValidLongitude = function(lng) {
        if (lng >= MIN_LNG && lng <= MAX_LNG) {
            return true;
        }

        return false;
    }

    this.encode = function(lat, lng) {
        return this.encodeToNBit(lat, lng, 12);
    }

    this.encodeToNBit = function(lat, lng, codeLength) {
        if (!this.isValidLatitude(lat) || !this.isValidLongitude(lng) || codeLength <= 3 || codeLength % 2 > 0) {
            return '';
        }

        var code = '';
        var latPrecision = Math.floor(codeLength / 2);
        var lngPrecision = codeLength - latPrecision;
        var latOffset = Math.floor((lat - originLat) * Math.pow(10, latPrecision - 2));
        var lngOffset = Math.floor((lng - originLng) * Math.pow(10, lngPrecision - 2));
        var modLng = Math.floor(lngOffset % 99);
        var modLat = Math.floor(latOffset % 99);

        if (modLng < 10) {
            code = '0' + modLng + code;
        }
        else {
            code = '' + modLng + code;
        }

        if (modLat < 10) {
            code = '0' + modLat + code;
        }
        else {
            code = '' + modLat + code;
        }

        lngOffset = Math.floor(lngOffset / 99);
        latOffset = Math.floor(latOffset / 99);

        for (var i = 1; i <= latPrecision - 2; i++) {
            code = Math.floor(lngOffset % 10) + code;
            code = Math.floor(latOffset % 10) + code;
            lngOffset = Math.floor(lngOffset / 10);
            latOffset = Math.floor(latOffset / 10);
        }

        return code;
    }

    this.decode = function(paraRitucode) {
        if (!paraRitucode || paraRitucode.length != 12) {
            return [-1, -1, -1, -1];
        }

        var lngOffset = 0.0;
        var latOffset = 0.0;
        var loopEnd = paraRitucode.length - 4;

        for (var i = 0; i < loopEnd; i += 2) {
            latOffset *= 10;
            latOffset += parseInt(paraRitucode.substr(i, 1), 10);
            lngOffset *= 10;
            lngOffset += parseInt(paraRitucode.substr(i + 1, 1), 10);
        }

        latOffset *= 99;
        latOffset += parseInt(paraRitucode.substr(paraRitucode.length - 4, 2), 10);
        var latPrecision = 1 / Math.pow(10, paraRitucode.length / 2 - 2);
        var lat = latOffset * latPrecision;
        lngOffset *= 99;
        lngOffset += parseInt(paraRitucode.substr(paraRitucode.length - 2, 2), 10);
        var lngPrecision = 1 / Math.pow(10, paraRitucode.length / 2 - 2);
        var lng = lngOffset * lngPrecision + 72;

        if (this.isValidLatitude(lat) && this.isValidLongitude(lng)) {
            var maxLng = lng + lngPrecision;
            maxLng = maxLng > 180 ? 180 : maxLng;
            var maxLat = lat + latPrecision;
            maxLat = maxLat > 90 ? 90 : maxLat;

            return [lng.toFixed(paraRitucode.length / 2 - 2),
                lat.toFixed(paraRitucode.length / 2 - 2),
                maxLng.toFixed(paraRitucode.length / 2 - 2),
                maxLat.toFixed(paraRitucode.length / 2 - 2)];
        }

        return [-1, -1, -1, -1];
    }
}
function enCode(gpsid) {
    var latlng = gpsid;
    var obj = new Array();
    var gpsidCode = new RituCode();
    return gpsidCode.decode(latlng);
}

function code() {
    try {
        var lat = document.getElementById("inputLat").value;
        var lng = document.getElementById("inputLng").value;
        var gpsidCode = new RituCode();
        var latlng = gpsidCode.encode(lat, lng);
        document.getElementById("code").innerHTML = latlng;
    } catch (e) {
        alert("出错");
    }
}